import com.mit.web.business.proxy.DesignProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.model.design.SpecialProcessEnquiryModel;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.design.SpecialProcessEditWindow;
import com.mit.web.vo.design.StyleVO;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.PopUpMenuButton;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.events.MenuEvent;
import mx.logging.ILogger;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;

private var _pm:IProgressMonitor;
private var _proxy:DesignProxy = DesignProxy.getInstance();
private var _logger:ILogger;
[Bindable] private var _model:SpecialProcessEnquiryModel;


private function datagridCreationCompleteHanlder(event:FlexEvent):void{
	_logger = LogUtil.getLogger(this);
	_logger.debug("DataGrid created, load all records");
	
	_model = new SpecialProcessEnquiryModel();
	doEnquiry();
}

private function doEnquiry(code:String = null, state:String = null, pageNo:int = 1):void{
	//读取记录集
	var r1:IInvokeResponder = _proxy.getPagedSpecTasks(pageNo, code, state);
	r1.addResultListener(retrieveListHandler);
	r1.addFaultListener(exceptionHandler);
	
	var r2:IInvokeResponder = _proxy.getSpecTaskCount(code, state);
	r2.addResultListener(retrieveRCHandler);
	r2.addFaultListener(exceptionHandler);
}

private function retrieveListHandler(event:ResultEvent):void{
	var lst:ArrayCollection = event.result as ArrayCollection;
	
	//有记录返回才更新，否则只是提示无记录存在
	if(lst != null && lst.length > 0){
		_model.styleLst = lst;
		dg.validateNow();
	}
	else
		MessageUtil.popNoRecordMessage(this);
}

private function retrieveRCHandler(event:ResultEvent):void{
	var rc:int = event.result as int;
	//与取回记录的行为保持一致，如果没有记录找到，不做任何动作。
	if(rc > 0){
		_logger.debug("Total record: " + rc.toString());
		_model.recordCount = rc;
	}
}

/**
 * 输入搜索条件后按回车键
 * */
private function doSearch():void{
	//跳回第一页
	_model.pageNo = 1;
	_model.searchCode =txt_search.text;	
	doEnquiry(_model.searchCode);
}

/**
 * 点击刷新按钮
 * */
private function doRefresh(event:MouseEvent):void{
	_model.searchCode = null;
	_model.searchState = null;
	_model.pageNo = 1;
	doEnquiry();
}

/**
 * 选择状态
 * */
private function doFilter(event:MenuEvent):void{
	_model.pageNo = 1;
	_model.searchState = event.item.data;
	_model.searchCode =txt_search.text;	
	//更改Popupmenubutton的标签
	(event.currentTarget as PopUpMenuButton).label = event.label;
	doEnquiry(_model.searchCode, _model.searchState,  _model.pageNo);
}

/**
 * 双击一条记录以编辑
 * */
private function doEdit(event:ListEvent):void{
	var item:Object = dg.selectedItem;
	if(item){
		var window:SpecialProcessEditWindow = new SpecialProcessEditWindow();
		window.addEventListener(PopWinCloseEvent.SAVED, editRecordHandler);
		window.title = item.code;
		window.style = item as StyleVO;
		PopUpManager.addPopUp(window,this,true);
		PopUpManager.centerPopUp(window);
		
	}
}

private function goToPage(pageNo:int):void{
	_model.pageNo = pageNo;
	doEnquiry(_model.searchCode, _model.searchState, _model.pageNo);
}

private function editRecordHandler(event:PopWinCloseEvent):void{
	//数据量小，不重新取数据，直接刷新datagrid
	var s:StyleVO = dg.selectedItem as StyleVO;

	s = event.vo as StyleVO;
	
	dg.validateNow();
	//显示成功提示
	showMessageTip(event.msg, dg);
}

